// src/router/index.js
import { createRouter, createWebHistory } from 'vue-router'
import Bed from "@/views/Bed.vue";
import BedRecord from "@/views/BedRecord.vue";

// 按需懒加载
const Login                   = () => import('@/views/Login.vue')
const UserList                 = () => import('@/views/UserList.vue')
const WelcomeLayout           = () => import('@/index.vue')
const WelcomeHome             = () => import('@/views/WelcomeHome.vue')
const CareWorker              = () => import('@/views/CareWorkerList.vue')
const MealCalendar            = () => import('@/views/MealCalendar.vue')
const MealRecord              = () => import('@/views/MealRecord.vue')
const MealSet                 = () => import('@/views/MealSet.vue')
const client                  = () => import('@/views/client.vue')
const checkOutRecord               = () => import('@/views/checkOutRecord.vue')
const outingRecord                = () => import('@/views/outingRecord.vue')
const ChangePassword                = () => import('@/views/ChangePassword.vue')
const clientCarer                = () => import('@/views/clientCarer.vue')
const ChangeProfile                = () => import('@/views/ChangeProfile.vue')

import careLevel from '../views/careLevel.vue'
import careItem from '../views/careItem.vue'
import clientCareItem from '../views/clientCareItem.vue'
import careRecord from '../views/careRecord.vue'
import visualization from '../views/visualization.vue'
import stream from '../views/Stream.vue'
const routes = [
    {
        path: '/login',
        name: 'Login',
        component: Login,
        meta: { guest: true, title: '登录' }
    },
    {
        path: '/',
        name: 'Layout',
        component: WelcomeLayout,
        meta: { requiresAuth: true },
        children: [
            {
                path: '',
                name: 'Welcome',
                component: WelcomeHome,
                meta: { title: '欢迎页', roles: ['健康管家','管理员'] }
            },
            {
                path: '/change-password',
                name: 'ChangePassword',
                component: ChangePassword,
                meta: { title: '初始密码修改', roles: ['健康管家','管理员'] }
            },
            {
                path: '/change-profile',
                name: 'ChangeProfile',
                component: ChangeProfile ,
                meta: { title: '个人信息修改', roles: ['健康管家','管理员'] }
            },
            {
                path: 'userList',
                name: 'UserList',
                component: UserList,
                meta: { title: '用户信息维护', roles: ['管理员'] }
            },
            {
                path: 'careWorker',
                name: 'CareWorker',
                component: CareWorker,
                meta: { title: '健康管家列表', roles: ['管理员'] }
            },
            {
                path: 'mealCalendar',
                name: 'MealCalendar',
                component: MealCalendar,
                meta: { title: '膳食日历', roles: ['管理员'] }
            },
            {
                path: 'mealSet',
                name: 'MealSet',
                component: MealSet,
                meta: { title: '客户膳食配置', roles: ['管理员','健康管家'] }
            },
            {
                path: 'mealRecord/:clientId?',  // 注意后面加了 ?
                name: 'MealRecord',
                component: MealRecord,
                props: true,
                meta: { title: '客户订餐记录', roles: ['管理员','健康管家'] }
            },
            {
                path: 'bed',
                name: 'Bed',
                component: Bed,
                meta: { title: '床位示意图', roles: ['管理员'] }
            },
            //zhq
            {
                path: 'client',
                name: 'client',
                component: client,
                meta: { title: '客户列表', roles: ['管理员'] }
            },
            {
                path: 'clientCarer',
                name: 'clientCarer',
                component: clientCarer,
                meta: { title: '客户列表', roles: ['健康管家'] }
            },
            {
                path: 'checkOutRecord',
                name: 'checkOutRecord',
                component: checkOutRecord,
                meta: { title: '退住列表', roles: ['健康管家','管理员'] },
            },
            {
                path: 'outingRecord',
                name: 'outingRecord',
                component: outingRecord,
                meta: { title: '外出列表', roles: ['健康管家','管理员'] },
            },
            //zhq
            {
                path: 'bedRecord',
                name: 'BedRecord',
                component: BedRecord,
                meta: { title: '床位记录管理', roles: ['管理员'] }
            },
            {
                path: 'careLevel',
                name: 'careLevel',
                component: careLevel,
                meta: { title: '护理级别管理', roles: ['管理员']  }
            },
            {
                path: 'careItem',
                name: 'careItem',
                component: careItem,
                meta: { title: '护理项目管理', roles: ['管理员']  }
            },
            {
                path: 'clientCareItem',
                name: 'clientCareItem',
                component: clientCareItem,
                meta: { title: '护理服务关注', roles: ['管理员']  }
            },
            {
                path: 'careRecord',
                name: 'careRecord',
                component: careRecord,
                meta: { title: '客户日常护理', roles: ['健康管家']  }
            },
            {
                path: 'visualization',
                name: 'visualization',
                component: visualization,
                meta: { title: '数据面板', roles: ['管理员']  }
            },
            {
                path: 'stream',
                name: 'stream',
                component: stream,
                // meta: { title: '流式智能聊天', roles: ['健康管家','管理员']  }
            }
        ]
    }
]

const router = createRouter({
    history: createWebHistory(),
    routes,
    scrollBehavior: () => ({ top: 0 })
})

// 全局前置守卫（同前面示例）
router.beforeEach((to, from, next) => {
    const token = localStorage.getItem('token')
    const role  = localStorage.getItem('role')  // '健康管家' or '管理员'

    if (to.meta.requiresAuth && !token) {
        return next({ name: 'Login', query: { redirect: to.fullPath }})
    }
    if (to.meta.guest && token) {
        return next({ name: 'Welcome' })
    }

    if (to.meta.roles) {
        if (!role) {
            return next({ name: 'Login' })
        }
        if (!to.meta.roles.includes(role)) {
            if (to.name === 'Welcome') return next()
            return next({ name: 'Welcome' })
        }
    }
    next()

})

export default router
